package com.weitu.contractor.service.impl;

import com.weitu.contractor.entity.ContractorStaff;
import com.weitu.contractor.entity.ContractorStaffCriteria;
import com.weitu.contractor.mapper.ContractorStaffMapper;
import com.weitu.contractor.service.ContractorStaffService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ContractorStaffServiceImpl implements ContractorStaffService {
    private final ContractorStaffMapper contractorStaffMapper;

    private static final Logger logger = LoggerFactory.getLogger(ContractorStaffServiceImpl.class);

    @Autowired
    public ContractorStaffServiceImpl(ContractorStaffMapper contractorStaffMapper) {
        this.contractorStaffMapper = contractorStaffMapper;
    }

    @Override
    public int countByCriteria(ContractorStaffCriteria example) {
        int count = this.contractorStaffMapper.countByCriteria(example);
        logger.debug("count: {}", count);
        return count;
    }

    @Override
    public ContractorStaff selectByPrimaryKey(String id) {
        return this.contractorStaffMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ContractorStaff> selectByCriteria(ContractorStaffCriteria example) {
        return this.contractorStaffMapper.selectByCriteria(example);
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(String id) {
        return this.contractorStaffMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(ContractorStaff record) {
        return this.contractorStaffMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional
    public int updateByCriteriaSelective(ContractorStaff record, ContractorStaffCriteria example) {
        return this.contractorStaffMapper.updateByCriteriaSelective(record, example);
    }

    @Override
    @Transactional
    public int insertSelective(ContractorStaff record) {
        return this.contractorStaffMapper.insertSelective(record);
    }

    @Override
    public boolean checkIfExist(String contractorId, String code, String idcard) {
        ContractorStaffCriteria criteria = new ContractorStaffCriteria();
        criteria.or().andNameEqualTo(code).andContractorIdEqualTo(contractorId).andDelStateEqualTo(0);
        criteria.or().andIdcardEqualTo(idcard).andContractorIdEqualTo(contractorId).andDelStateEqualTo(0);
        return contractorStaffMapper.countByCriteria(criteria) > 0;
    }

    @Override
    public List<String> selectIdsByName(String name) {
        List<String> Ids = new ArrayList<String>();
        ContractorStaffCriteria criteria = new ContractorStaffCriteria();
        criteria.createCriteria().andNameLike("%" + name + "%");
        List<ContractorStaff> contractorStaffList = contractorStaffMapper.selectByCriteria(criteria);
        if (contractorStaffList.isEmpty()) {
            return Ids;
        }
        for (ContractorStaff contractorStaff : contractorStaffList) {
            Ids.add(contractorStaff.getId());
        }
        return Ids;
    }
}